// 1.队列
class Queue {
    constructor() {
        this.count = 0
        this.lowCount = 0
        this.item = {}
    }
    enqueue(element) {//入队
        this.item[this.count] = element
        this.count++
    }
    isEmpty() {//判断有没有
        return this.count - this.lowCount === 0
    }
    dequeue() {//出队
        if (this.isEmpty) {
            return undefined
        }
        const result = this.item[this.lowCount]
        delete this.item[this.lowCount]
        this.lowCount++
        return result
    }
    size() {//队列长度
        return this.count - this.lowCount
    }
    toString() {//转字符串
        if (this.isEmpty) {
            return ''
        }
        let srt = ""
        for (let i = this.lowCount; i < this.count; i++) {
            srt += this.item[i]
        }
        return srt
    }
}
let q = new Queue()
console.log(q.isEmpty())
q.enqueue("a")
q.enqueue("b")
q.enqueue("c")
console.log(q.isEmpty())
q.dequeue()
q.dequeue()
q.dequeue()
console.log(q.size())
//2.
function jigu(list, num) {//list:列表  num:循环数

    let failList = []//失败列表
    for (let i = 0; i < list.length; i++) {
        q.enqueue(list[i])//入队
    }
    while (q.size() > 1) {
        for (let i = 0; i < num; i++) {
            list.push(q.dequeue)//出队
        }
    }
    return {
        reduce: q.dequeue(),
        list: list
    }
}
const list = ["a", "b", "c", "d", "e"]
const result = jigu(list, 3)
console.log(result);
//3.
class Node {
    constructor(element) {
        this.element = element
        this.right = null
        this.left = null
    }
}
class Tree {
    constructor() {
        this.root = null
    }
    insert(element) {
        let node = new Node(element)
        if (this.root === null) {
            this.root = node
        } else {
            this.insertNode(node, this.root)
        }
    }
    insertNode(newNode, rootNode) {
        if (newNode.element < rootNode.element) {
            if (rootNode.left === null) {
                rootNode.left = newNode
            } else {
                this.insertNode(newNode, rootNode.left)
            }
        } else {
            if (rootNode.right === null) {
                rootNode.right = newNode
            } else {
                this.insertNode(newNode, rootNode.right)
            }
        }
    }
    min() {
        return this.minNode(this.root)
    }
    minNode(rootNode) {
        if (rootNode === null) {
            return null
        }
        while (rootNode.left != null) {
            rootNode = rootNode.left
        }
        return rootNode
    }
    max() {
        return this.maxNode(this.root)
    }
    maxNode(rootNode) {
        if (rootNode === null) {
            return null
        }
        while (rootNode.right != null) {
            rootNode = rootNode.right
        }
        return rootNode
    }
    //4.查找
    search(element){
        this.searchNode(this.root,element)
    }
    searchNode(node,element){
        if(node==null){
            return false//未查找到
        }
        if(node.element>element){
            return this.searchNode(node.left,element)
        }else if(node.element<element){
            return this.searchNode(node.right,element)
        }else{
            return true//查找到
        }
    }
    //5.翻转二叉搜索树
    invertTree(node = this.root) {
        if (node === null) {
            return
        }
        this.invertTree(node.left)
        this.invertTree(node.right)
        this.exchange(node)
    }

    exchange(node) {
        let temp = node.left
        node.left = node.right
        node.right = temp
    }

}
let t = new Tree()
